<?php
/**
 * polwebgis
 * 
 * This file is part of polwebgis.
 *
 * @copyright   Copyright (c) 2012 Dominik Janisch (http://www.joanneum.at)
 * @lastmodified    $LastChangedDate: 2012-03-07 09:15:12 +0000 (Wed, 07 Mar 2012) $
 * @revision    $Revision: 160 $ 
 */

class MapClassProperties
{

    private $min_;
    private $max_;
    private $range_;
    private $width_;
    private $avg_width_float_;
    private $cases_;
    private $is_int_;
    private $class_width_;
    private $stddev_;
    private $mean_;
    private $decimals_;
    
    const DECIMALS = 5;

    public function __construct($min, $max, $cases, $stddev, $mean, $class_width, $is_int)
    {
        $this->cases_ = $cases;
        $this->decimals_ = constant("self::DECIMALS");
        $this->is_int_ = $is_int;
        $this->stddev_ = $stddev;
        $this->mean_ = $mean;
        $this->mean_ = $this->roundDecimals($mean);
        if($this->is_int_)
        {
            $this->max_ = $max+1;
            $this->min_ = floor($min);
        }
        else
        {
            $this->decimals_ = $this->getAmountOfDecimals($max, $min);
            $this->max_ = $this->ceilDecimals($max);
            $this->min_ = floor($min*pow(10, $this->decimals_))/pow(10,$this->decimals_);
        }
        $this->range_ = $this->max_ - $this->min_;
        $this->setClassWidth($class_width);
    }
        
    public function __destruct(){}
    
    private function getAmountOfDecimals($max, $min)
    {
        $max_pos = strpos($max, '.');
        $max_decimals = 0;
        $min_pos = strpos($min, '.');
        $min_decimals = 0;
        if($max_pos !== false)
            $max_decimals = $this->getDecimalsLength($max, $max_pos);
        if($min_pos !== false)
            $min_decimals = $this->getDecimalsLength($min, $min_pos);
        $decimals = $min_decimals < $max_decimals ? $min_decimals : $max_decimals;
        return $decimals > constant("self::DECIMALS") ? constant("self::DECIMALS") : $decimals-1;
    }
    
    private function getDecimalsLength($string, $pos)
    {
        $digits = substr($string, $pos+1);
        if($digits !== false)
            return strlen($digits);
        return 0;
    }
    
    public function ceilDecimals($number)
    {
        return ceil($number*pow(10, $this->decimals_))/pow(10,$this->decimals_);
    }
    
    public function roundDecimals($number)
    {
        return round($number, $this->decimals_);
    }
    
    public function getMin()
    {
        return $this->min_;
    }
    
    public function getMax()
    {
        return $this->max_; 
    }
    
    public function getRange()
    {
        return $this->range_; 
    }
    
    public function getWidth()
    {
        return $this->width_; 
    }
    public function getCases()
    {
        return $this->cases_; 
    }
    
    public function getAvgWidthFloat()
    {
        return $this->avg_width_float_; 
    }
    
    public function getMean()
    {
        return $this->mean_;
    }
    
    public function getStddev()
    {
        return $this->stddev_;
    }
    
    public function setJenksMin()
    {
        if($this->is_int_)
            --$this->min_;
        else
            $this->min_ = $this->min_ - 0.001;
    }
    
    public function setClassWidth($class_width)
    {
        $this->class_width_ = $this->roundDecimals($class_width);
        $this->updateWidthProperties();
    }
    
    public function setWidth()
    {
        if($this->is_int_)
            $this->width_ = ceil($this->range_ / $this->class_width_);
        else
            $this->width_ = $this->range_ / $this->class_width_;
    }
    
    private function updateWidthProperties()
    {
        $this->setWidth();
        $this->avg_width_float_ =  $this->cases_ / $this->class_width_;
    }
}

?>
